<?php
namespace app\manager\controller;

use think\Controller;

class Manager extends Controller
{
    /**
     * 验证用户是否处于登录状态
     */
    protected function _initialize()
    {
        $user = session('Muser');
        if(!$user){
            $this->redirect('login/index');
        }
    }


    public function index()
    {
        $adlist = db('manager')->select();
        $this->assign('adlist',$adlist);
        return $this->fetch('index/admin_list');
    }

    public function admin_add()
    {
        return $this->fetch('index/admin_add');
    }

    public function admin_add_do()
    {
        $param = input('post.');

        if(empty($param['user_name'])){
            return "用户名不能为空！";
        }
        if(empty($param['nickname'])){
            return "昵称不能为空！";
        }

        if(empty($param['user_password'])){

            return "密码不能为空！";
        }
        $password=md5($param['user_password']);
        $data = ['m_name' => $param['user_name'],'m_nick' => $param['nickname'], 'm_pass' => $password];
        db('manager')->insert($data);
        echo 200;

    }

    public function admin_edit()
    {
        $param = input('get.');
        $adcont = db('manager')->where('m_id',$param['id'])->find();
        $this->assign('adcont',$adcont);
        return $this->fetch('index/admin_edit');
    }

    public function admin_edit_do()
    {
        $param = input('post.');

        if(empty($param['user_name'])){
            return "用户名不能为空！";
        }
        if(empty($param['nickname'])){
            return "昵称不能为空！";
        }
        if(empty($param['user_password'])){

            return "密码不能为空！";
        }
        db('manager')
            ->where('m_id', $param['user_id'])
            ->update([
                'm_name'  => $param['user_name'],
                'm_nick'  => $param['nickname'],
                'm_pass' => md5($param['user_password']),
            ]);
        echo 200;
    }

    public function admin_dele()
    {
        $param = input('post.');
        db('manager')->where('m_id',$param['id'])->delete();
        echo "删除成功！";
    }
}
